package com.kaifamiao.iterator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * java.util.Iterator<E>:
 * 1、boolean hasNext()
 * 2、E next()
 */
public class TestIterator {

    public static void main(String[] args) {

        Collection<String> names = new ArrayList<>();
        names.add( "孙悟空" );
        names.add( "孙行者" );
        names.add( "者行孙" );
        names.add( "行者孙" );
        System.out.println( names.toString() );

        // 获得用来 "迭代" names 集合的 "迭代器"
        Iterator<String> itor = names.iterator();
        // 用 "迭代器"(itor) 迭代 names 集合
        while( itor.hasNext() ) {
            String e = itor.next();
            System.out.println( e );
        }

        System.out.println( itor.hasNext() ); // false

        // 重新获得迭代器
        itor = names.iterator();
        for( ; itor.hasNext() ; ){
            String e = itor.next();
            System.out.println( e );
        }

    }

}
